package com.maoye.ossp.sys.news.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.org.api.model.User;
import com.hotent.org.api.service.UserService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.news.entity.News;
import com.maoye.ossp.sys.news.entity.NewsCategory;
import com.maoye.ossp.sys.news.service.INewsCategoryService;
import com.maoye.ossp.sys.news.service.INewsService;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 新闻Controller
 * 
 * @author yanzy
 * 
 *  
 */
@Controller
@RequestMapping(value = "/sys/news/new/")
public class NewsController extends BaseController {
	@Resource
	private INewsService newsService;
	
	@Resource
	private INewsCategoryService InewsCategoryService;
	
	@Resource
	private UserService userservice;

	/**
	 * 得到所有的新闻列表
	 * 
	 * @param news
	 *            实体参数
	 * @param model
	 * @return 字符 (页面的路径)
	 */
	@RequestMapping(value = "list")
	public String list(News news, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String content = request.getParameter("content");
		news.setContent(content);
		QueryFilter queryFilter = getQuerFilter(request,news);
		Page<News> page = (Page<News>)newsService
				.find(queryFilter);

		model.addAttribute("page", page);
		return "/sys/news/new/list";

	}

	/**
	 * 添加新闻的方法
	 * 
	 * @param news
	 *            参数
	 * @param model
	 *            模型
	 * @param redirectAttributes
	 *            跳转的类型
	 * @return String
	 */
	@RequestMapping(value = "save")
	public String save(News news, HttpServletRequest request,
			HttpServletResponse response, Model model,
			RedirectAttributes redirectAttributes) {

		if (news.getId() == null || news.getId() == "") {

			news.setId(idGenerator.getUId().toString());
			news.setCreateBy(UserUtils.getCurrentUserId().toString());
			news.setCreateTime(new Date());
			newsService.create(news);

		} else {
         
			news.setUpdateBy(UserUtils.getCurrentUserId().toString());
			news.setCreateBy(news.getCreateBy());
			news.setCreateTime(news.getCreateTime());
			news.setUpdateTime(new Date());
			newsService.update(news);

		}
		addMessage(redirectAttributes, "保存新闻'" + news.getTitle() + "'成功");
		return "redirect:/sys/news/new/list";
	}

	/**
	 * 首页跳转到添加的页面的方法
	 * 
	 * @param news
	 *            参数
	 * @param model
	 *            模型
	 * @return String
	 */
	@RequestMapping(value = "add")
	public String add(News news, Model model) {
		List<NewsCategory> list = InewsCategoryService.getAll();
		model.addAttribute("categoryList",list);
		return "/sys/news/new/add";
	}

	/**
	 * 修改新闻数据
	 * 
	 * @param id
	 *            根据主键修改(参数)
	 * @param model
	 *            模型
	 * @return String
	 */
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		News news = newsService.get(id);
		//获得修改人
		User currentUser = userservice.getById(UserUtils.getCurrentUserId().toString());
		news.setUpdateBy(UserUtils.getCurrentUserId().toString());
		news.setUpdateTime(new Date());
		news.setUpdateName(currentUser.getFullname());
		news.setCreateBy(news.getCreateBy());
		news.setCreateTime(news.getCreateTime());
		List<NewsCategory> list = InewsCategoryService.getAll();
		model.addAttribute("categoryList",list);
		model.addAttribute("news", news);
		return "/sys/news/new/update";
	}

	/**
	 * 查看信息
	 * 
	 * @param id
	 *            参数
	 * @param model
	 *            模型
	 * @return String
	 */
	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model) {
		News news = newsService.get(id);
		model.addAttribute("news", news);
		return "/sys/news/new/view";
	}

	/**
	 * 删除新闻
	 * 
	 * @param id
	 *            参数
	 * @param model
	 *            模型
	 * @param redirectAttributes
	 *            跳转类型
	 * @return String
	 */
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) {
		newsService.remove(id);
		addMessage(redirectAttributes, "删除新闻成功");
		return "redirect:/sys/news/new/list";
	}

	/**
	 * 置顶
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{id}/updateTop")
	public String updateOne(@PathVariable String id,
			HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes, Model model, Map map) {
		News news = newsService.get(id);
		news.setIsTop("1");
		news.setTopTime(new Date());
		news.setValidTime(new Date());
		news.setUpdateBy(news.getCreateBy());
		news.setUpdateTime(new Date());
		newsService.updateOne(news);
		model.addAttribute("news", news);
		this.listTop(news, request, response, model, map);
		addMessage(redirectAttributes, "置顶成功");

		return "redirect:/sys/news/new/list";

	}

	@RequestMapping(value = "selectTop")
	public String listTop(News news, HttpServletRequest request,
			HttpServletResponse response, Model model, Map map) {

		List<News> list = newsService.selectTop(map);
	
		model.addAttribute("list", list);

		return "/sys/news/new/list";

	}
	
	/**
	 * 查询新闻信息至前端
	 * @param response
	 * @throws IOException
	 * 
	 */
	@RequestMapping(value = "/getForward")
	public void getForward(HttpServletResponse response) throws IOException{
		List<News> list = newsService.getForward();
		PrintWriter out=response.getWriter();  
        JSONArray jsonArray=JSONArray.fromObject(list);  
        out.print(jsonArray);  
        out.flush();  
        out.close();
	}
	
	/**
	 * 对特殊字符进行处理
	 * @param content
	 * @return
	 * 
	 */
	public String formatCharacter(String content) {
		content = content.replace("&lt;", "<");
		content = content.replace("&gt;", ">");
		content = content.replace("&amp;", "&");
		content = content.replace("&quot;", "\"");
		content = content.replace("&reg;", "");
		content = content.replace("&copy;", "");
		content = content.replace("&trade;", "");
		content = content.replace("&yen", "¥");
		content = content.replace("&ldquo;", "“");
		content = content.replace("&rdquo;", "”");
		content = content.replace("&permil;", "%");
		content = content.replace("&#39;", "'");
		return content;
	}
	
	/**
	 * 前端查询详细新闻信息
	 * @param id
	 * @param request
	 * @param model
	 * 
	 */
	@RequestMapping(value = "/{id}/getDetail")
	public String getDetail(@PathVariable String id,HttpServletRequest request,Model model){
		News news = newsService.get(id);
		Long count = news.getClickCount();
		if(news.getClickCount()==null){
			count = new Long(1);
		}
		else {
			count = count+1;
		}
		news.setClickCount(count);
		//修改点击次数
		newsService.updateCount(news);
		String content = news.getContent();
		content = formatCharacter(content);
		news.setContent(content);
		model.addAttribute("news", news);
		return "/sys/news/NewsDetail";
	}
	
	/**
	 * 更多新闻信息
	 * 
	 * @param news
	 *            实体参数
	 * @param model
	 * @return 字符 (页面的路径)
	 */
	@RequestMapping(value = "/MoreNews")
	public String MoreNews(News news, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		QueryFilter queryFilter = getQuerFilter(request,news);
		Page<News> page = (Page<News>)newsService
				.find(queryFilter);
		model.addAttribute("page", page);
		return "/sys/news/MoreNews";

	}
}
